import {
    _decorator,
    Animation,
    BoxCollider,
    CapsuleCollider,
    Collider,
    Component,
    CylinderCollider,
    ICollisionEvent,
    ITriggerEvent,
    MeshCollider,
    Node,
    SphereCollider,
} from 'cc';
import {ColliderGroupEnum} from "db://assets/scripts/game/ColliderGroupEnum";
import {LaserEffect} from "db://assets/scripts/game/effect/monsterSkill/LaserEffect";
import {EnergyBallEffect} from "db://assets/scripts/game/effect/monsterSkill/EnergyBallEffect";
import {FireBallBigEffect} from "db://assets/scripts/game/effect/monsterSkill/FireBallBigEffect";
import {GameManager} from "db://assets/scripts/game/GameManager";
import {FireBallEffect} from "db://assets/scripts/game/effect/monsterSkill/FireBallEffect";
import {JetFiresEffect} from "db://assets/scripts/game/effect/monsterSkill/JetFiresEffect";
import {TornadoEffect} from "db://assets/scripts/game/effect/monsterSkill/TornadoEffect";
import {PoolManager} from "db://assets/scripts/common/PoolManager";
import {DispersionSurroundEffect} from "db://assets/scripts/game/effect/monsterSkill/DispersionSurroundEffect";
import {DispersionEffect} from "db://assets/scripts/game/effect/monsterSkill/DispersionEffect";
import {GameState} from "db://assets/scripts/game/GameState";

const {ccclass, property} = _decorator;

/**
 * 碰撞体名称
 */
enum COLLIDER_NAME {
    /**
     * 直线飞行能量球
     */
    ENERGY_BALL = 1,
    /**
     * 线飞行的小火球
     */
    FIRE_BALL = 2,
    /**
     * 直线范围型的火焰
     */
    JET_FIRES = 3,
    /**
     * 180度散射
     */
    DISPERSION = 4,
    /**
     * 旋转前进的龙卷风
     */
    TORNADO = 5,
    /**
     * 直线下坠的大火团
     */
    FIRE_BALL_BIG = 6,
    /**
     * 360度六角形散射
     */
    DISPERSION_SURROUND = 7,
    /**
     * 直线激光
     */
    LASER = 8,
}

/**
 * 怪物武器碰撞器/触发器脚本
 */
@ccclass('MonsterSkillColliderController')
export class MonsterSkillColliderController extends Component {
    /**
     * 碰撞体/触发器类型名称
     */
    @property({displayOrder: 1})
    public colliderName: any = COLLIDER_NAME.ENERGY_BALL;

    /**
     * 碰撞体
     */
    public colliderCom: Collider;
    /**
     * 动画
     */
    public animation: Animation;

    /**
     * 碰撞体名称
     */
    public static COLLIDER_NAME = COLLIDER_NAME;

    /**
     * 是否处在龙火焰中
     */
    private isOnJetFires: boolean = false;
    /**
     * 大龙火焰检查间隔
     */
    private checkInterval: number = 1;

    onLoad() {
        this.colliderCom = this.node.getComponent(BoxCollider)
            || this.node.getComponent(CylinderCollider)
            || this.node.getComponent(SphereCollider)
            || this.node.getComponent(CapsuleCollider)
            || this.node.getComponent(MeshCollider)
            || this.node.getComponent(CylinderCollider);

        if (!this.colliderCom) {
            console.error("this node does not have collider component");
        }
    }

    onEnable() {
        if (this.colliderName === COLLIDER_NAME.JET_FIRES) {
            this.colliderCom.on("onTriggerStay", this.onTriggerStayCb, this);
            this.colliderCom.on("onTriggerExit", this.onTriggerExitCb, this);
        } else {
            if (this.colliderCom.isTrigger) {
                this.colliderCom.on('onTriggerEnter', this.onTriggerEnterCb, this);
            } else {
                this.colliderCom.on('onCollisionEnter', this.onCollisionEnterCb, this);
            }
        }

    }

    onDisable() {
        if (this.colliderName === COLLIDER_NAME.JET_FIRES) {
            this.colliderCom.off("onTriggerStay", this.onTriggerStayCb, this);
            this.colliderCom.off("onTriggerExit", this.onTriggerExitCb, this);
        } else {
            if (this.colliderCom.isTrigger) {
                this.colliderCom.off('onTriggerEnter', this.onTriggerEnterCb, this);
            } else {
                this.colliderCom.off('onCollisionEnter', this.onCollisionEnterCb, this);
            }
        }
    }

    start() {
        this.isOnJetFires = false;
    }

    update(deltaTime: number) {
        if (GameManager.getInstance().gameStateProxy.getGameState() === GameState.OVER
            || !GameManager.getInstance().gamePlayerProxy.getPlayerNode() || !this.node.parent) {
            return;
        }

        //检测是否在龙焰（持续性技能里面），每隔0.5秒时间扣减一定伤害
        if (this.colliderName === COLLIDER_NAME.JET_FIRES && this.isOnJetFires) {
            this.checkInterval += deltaTime;
            if (this.checkInterval >= 0.5) {
                this.checkInterval = 0;

                let scriptSkillCollider = this.node.parent.getComponent(JetFiresEffect);
                this.hitPlayer(scriptSkillCollider.baseInfo);
            }
        }
    }

    /**
     * 初始化
     */
    public init() {

    }

    private onTriggerEnterCb(event: ITriggerEvent) {
        this.hitTarget(event.otherCollider);
    }

    private onCollisionEnterCb(event: ICollisionEvent) {
        this.hitTarget(event.otherCollider);
    }

    private hitTarget(otherCollider: any) {
        if (GameManager.getInstance().gameStateProxy.getGameState() === GameState.OVER
            || GameManager.getInstance().gameStateProxy.getGameState() !== GameState.PLAYING) {
            return;
        }

        if (otherCollider.getGroup() === ColliderGroupEnum.OBSTACLE) {
            //技能碰到游戏中的障碍则回收
            let scriptSkillCollider: any = null;

            switch (this.colliderName) {
                case COLLIDER_NAME.ENERGY_BALL:
                    scriptSkillCollider = this.node.getComponent(EnergyBallEffect);
                    if (!scriptSkillCollider.skillInfo.penetrate) {
                        PoolManager.getInstance().putNode(this.node);
                    }
                    break;
                case COLLIDER_NAME.DISPERSION:
                    scriptSkillCollider = this.node.getComponent(DispersionEffect);
                    if (!scriptSkillCollider.skillInfo.penetrate) {
                        scriptSkillCollider.hide();
                    }
                    break;
                case COLLIDER_NAME.TORNADO:
                    scriptSkillCollider = this.node.parent?.getComponent(TornadoEffect);
                    if (!scriptSkillCollider.skillInfo.penetrate) {
                        PoolManager.getInstance().putNode(this.node.parent as Node);
                    }
                    break;
                case COLLIDER_NAME.DISPERSION_SURROUND:
                    scriptSkillCollider = this.node.getComponent(DispersionSurroundEffect);
                    if (!scriptSkillCollider.skillInfo.penetrate) {
                        scriptSkillCollider.hide();
                    }
                    break;
            }
        } else if (otherCollider.getGroup() == ColliderGroupEnum.PLAYER && GameManager.getInstance().gamePlayerProxy.getPlayerNode()) {
            let scriptSkillCollider: any = null;

            switch (this.colliderName) {
                case COLLIDER_NAME.ENERGY_BALL:
                    PoolManager.getInstance().putNode(this.node);

                    scriptSkillCollider = this.node.getComponent(EnergyBallEffect);
                    this.hitPlayer(scriptSkillCollider.baseInfo);
                    break;
                case COLLIDER_NAME.FIRE_BALL:
                    //不在这里回收节点.在fireBall里面会回收
                    scriptSkillCollider = this.node.parent?.getComponent(FireBallEffect);
                    this.hitPlayer(scriptSkillCollider.baseInfo);
                    break;
                case COLLIDER_NAME.DISPERSION:
                    //注意这里不回收节点，只回收父节点
                    scriptSkillCollider = this.node.getComponent(DispersionEffect);
                    scriptSkillCollider.hide();

                    this.hitPlayer(scriptSkillCollider.baseInfo);
                    break;
                case COLLIDER_NAME.TORNADO:
                    scriptSkillCollider = this.node.parent?.getComponent(TornadoEffect);
                    this.hitPlayer(scriptSkillCollider.baseInfo);
                    break;
                case COLLIDER_NAME.FIRE_BALL_BIG:
                    scriptSkillCollider = this.node.parent?.getComponent(FireBallBigEffect);
                    this.hitPlayer(scriptSkillCollider.baseInfo);
                    break;
                case COLLIDER_NAME.DISPERSION_SURROUND:
                    //注意这里不回收，只回收父节点
                    scriptSkillCollider = this.node.getComponent(DispersionSurroundEffect);
                    scriptSkillCollider.hide();
                    this.hitPlayer(scriptSkillCollider.baseInfo);
                    break;
                case COLLIDER_NAME.LASER:
                    scriptSkillCollider = this.node.parent?.getComponent(LaserEffect);
                    this.hitPlayer(scriptSkillCollider.baseInfo);
                    break;
            }
        }
    }

    private hitPlayer(baseInfo: any) {
        if (!baseInfo) {
            console.warn("###找不到技能来源敌人", this.colliderName);
            return;
        }
        // console.log("###技能碰到玩家了", this.colliderName);
        GameManager.getInstance().gamePlayerProxy.getPlayerController().reduceBlood(baseInfo);
    }

    private onTriggerStayCb(event: ITriggerEvent) {
        if (event.otherCollider.getGroup() == ColliderGroupEnum.PLAYER && GameManager.getInstance().gamePlayerProxy.getPlayerNode()) {
            this.isOnJetFires = true;
        }
    }

    private onTriggerExitCb(event: ITriggerEvent) {
        if (event.otherCollider.getGroup() == ColliderGroupEnum.PLAYER && GameManager.getInstance().gamePlayerProxy.getPlayerNode()) {
            this.isOnJetFires = false;
        }
    }
}

